package com.xiaomaoaixue.wecom.domain.dto;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.mchange.lang.LongUtils;
import com.xiaomaoaixue.common.core.enums.UserStatus;
import com.xiaomaoaixue.common.core.enums.UserType;
import com.xiaomaoaixue.common.core.utils.StringUtils;
import com.xiaomaoaixue.system.api.domain.SysDept;
import com.xiaomaoaixue.system.api.domain.SysPost;
import com.xiaomaoaixue.system.api.domain.SysUser;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 通讯录用户列表
 * @author: HaoN
 * @create: 2020-10-17 00:19
 **/
@Data
public class WeUserListDto extends WeResultDto{

    private List<WeUserDto> userlist;


    public List<SysUser>  getSysUsers(){
        List<SysUser> weUsers=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(userlist)){
            userlist.forEach(user -> {
                SysUser weUser=new SysUser();
                //BeanUtil.copyProperties(user,weUser);
                weUser.setNickName(user.getName()+"("+user.getAlias()+")");
                weUser.setUserName(user.getUserid());
                weUser.setOpenId(user.getOpen_userid());
                weUser.setUserType(UserType.WECOME.getCode());
                // 1=已激活，2=已禁用，4=未激活，5=退出企业。
                weUser.setStatus(UserStatus.getUserStatusByWeStatus(user.getStatus()).getCode());
                weUser.setAvatar(user.getAvatar());
                weUser.setCreateTime(new Date());
                weUser.setSex(user.getGender().toString());
                weUser.setEmail(user.getEmail());
                weUser.setPhonenumber(user.getMobile());
                //职务 position  企业微信是直接文本， 这边是多对多， 暂时不同步吧
                if(StringUtils.isNotEmpty(user.getPosition())){
                    List<SysPost> posts=new ArrayList<>();
                    SysPost sysPost = new SysPost();
                    sysPost.setPostName(user.getPosition());
                    posts.add(sysPost);

                    weUser.setPosts(posts);
                }

                // 直属上级
                if (CollectionUtil.isNotEmpty(user.getDirect_leader()) &&
                        user.getDirect_leader().size()>0) {
                    weUser.setDirectLeader(user.getDirect_leader().get(0));
                }
                // 部门
                if (CollectionUtil.isNotEmpty(user.getDepartment()) &&
                        user.getDepartment().size()>0){
                    if(NumberUtil.isLong(user.getDepartment().get(0))) {
                        weUser.setDeptId(Long.valueOf(user.getDepartment().get(0)));
                    }
                }
                if (CollectionUtil.isNotEmpty(user.getIs_leader_in_dept()) &&
                        user.getIs_leader_in_dept().size()>0) {
                    if (NumberUtil.isLong(user.getIs_leader_in_dept().get(0))) {
                        weUser.setIsLeaderInDept(Integer.getInteger(user.getIs_leader_in_dept().get(0)));
                    }
                }
                weUsers.add(weUser);
            });
        }
        return weUsers;
    }

}
